"""
Test configuration and fixtures for the Wan 2.2 API tests.
"""
import os
import pytest
from fastapi.testclient import TestClient
from unittest.mock import patch, MagicMock

# Set test environment variables
os.environ["AUTODL_API_KEY"] = "test_api_key"
os.environ["AUTODL_ENDPOINT"] = "https://api.test.autodl.com"
os.environ["MAX_NODES"] = "2"

# Import app after setting environment variables
from main import app, tasks_db, task_queue, autodl_client, wan_manager

@pytest.fixture(scope="function")
def test_client():
    """Create a test client for the FastAPI application."""
    with TestClient(app) as client:
        yield client
    
    # Clean up after each test
    tasks_db.clear()
    task_queue.clear()

@pytest.fixture(scope="function")
def mock_autodl():
    """Mock the AutoDL client for testing."""
    with patch('main.autodl_client') as mock:
        mock_instance = MagicMock()
        mock_instance.create_instance.return_value = {
            "id": "test_instance_123",
            "status": "running",
            "ip": "192.168.1.100",
            "port": 8080
        }
        mock.return_value = mock_instance
        yield mock_instance

@pytest.fixture(scope="function")
def mock_wan_manager():
    """Mock the WanInstanceManager for testing."""
    with patch('main.wan_manager') as mock:
        mock_instance = MagicMock()
        mock_instance.max_instances = 2
        mock_instance.active_instances = {}
        mock_instance.create_instance.return_value = {
            "id": "test_instance_123",
            "status": "running",
            "ip": "192.168.1.100",
            "port": 8080
        }
        mock.return_value = mock_instance
        yield mock_instance

@pytest.fixture(scope="function")
def sample_task():
    """Return a sample task for testing."""
    return {
        "task_id": "test_task_123",
        "task_type": "T2V",
        "status": "queued",
        "prompt": "A beautiful sunset over mountains",
        "created_at": 1678901234.56789,
        "updated_at": 1678901234.56789
    }
